// GridViewAdapter.java
package com.sgb.kjwl.view.ui.adapter.shopping;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.MaterialCompanyEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tian on 12/7/15.
 */
public class GridViewAdapter extends ArrayAdapter<MaterialCompanyEntity.GoodsListBean> {

    private Context mContext;
    private int layoutResourceId;
    private ArrayList<MaterialCompanyEntity.GoodsListBean> mGridData = new ArrayList<MaterialCompanyEntity.GoodsListBean>();
    private RequestOptions options;
    private OnItemClick onItemClick;

    public GridViewAdapter(Context context, int resource, List<MaterialCompanyEntity.GoodsListBean> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.layoutResourceId = resource;
        this.mGridData = (ArrayList<MaterialCompanyEntity.GoodsListBean>) objects;
        options = new RequestOptions()
                .placeholder(R.drawable.null_img_list)//图片加载出来前，显示的图片
                .error(R.drawable.shop_default);//图片加载失败后，显示的图片

    }

    public void setGridData(ArrayList<MaterialCompanyEntity.GoodsListBean> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.name);
            holder.imageView = convertView.findViewById(R.id.image);
            convertView.findViewById(R.id.ll_container).setOnClickListener(v -> {
                if (onItemClick != null) {
                    onItemClick.itemClick(mGridData.get(position));
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        MaterialCompanyEntity.GoodsListBean item = mGridData.get(position);
        holder.textView.setText(item.getTitle());
        Glide.with(mContext).load(item.getImageUrl()).apply(options).into((holder.imageView));
        return convertView;
    }

    private class ViewHolder {
        TextView textView;
        ImageView imageView;
    }

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick {
        void itemClick(MaterialCompanyEntity.GoodsListBean goodsListBean);
    }
}
